﻿using System;
using System.Windows.Forms;

namespace Peepster
{
    public partial class InputView : Form, IInputView
    {
        private const int HtCaption = 0x2;
        private const int HtClient = 0x1;
        private const int MaxLength = 140;
        private const int WmNchittest = 0x84;
        private readonly InputPresenter _presenter;

        public InputView()
        {
            _presenter = new InputPresenter(this);
            InitializeComponent();
        }

        public void SetErrorMessage(string message)
        {
            MessageBox.Show(message);
        }

        public void ShowView()
        {
            Show();
        }

        public void HideView()
        {
            if (InvokeRequired)
            {
                BeginInvoke(new MethodInvoker(Hide));
                return;
            }

            Hide();
        }

        public void ClearInput()
        {
            textBoxMessage.Clear();
        }

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case WmNchittest:
                    base.WndProc(ref m);
                    if ((int) m.Result == HtClient)
                        m.Result = (IntPtr) HtCaption;
                    return;
            }
            base.WndProc(ref m);
        }

        private void textBoxMessage_TextChanged(object sender, EventArgs e)
        {
            labelCharacterCount.Text = (MaxLength - (textBoxMessage.Text.Length)).ToString();
        }

        private void textBoxTweetText_KeyDown(object sender, KeyEventArgs e)
        {
            HandleEscapeKey(e);
            HandleEnterKey(e);
        }

        private void HandleEscapeKey(KeyEventArgs key)
        {
            if (key.KeyCode != Keys.Escape) return;

            key.Handled = true;
            Hide();
        }

        private void HandleEnterKey(KeyEventArgs key)
        {
            if (key.KeyCode != Keys.Return) return;

            key.Handled = true;
            if (textBoxMessage.Text != String.Empty)
                _presenter.SendTweet(textBoxMessage.Text);
        }

        private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var settings = new SettingsView();
            settings.ShowDialog(this);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            notifyIcon.Visible = false;
            Close();
        }

        private void InputView_Load(object sender, EventArgs e)
        {
            _presenter.Initialize();
        }

        private void InputView_FormClosing(object sender, FormClosingEventArgs e)
        {
            _presenter.Close();
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            new AboutForm().ShowDialog();
        }
    }
}